//
//  SAPirntTopUpBill.m
//  saasRetail
//
//  Created by mac on 2023/2/9.
//

#import "SAPirntTopUpBill.h"
#import "SASalesOrderViewListModel.h"
#import "SACommoditySpecialFeesModel.h"
#import "NSString+Print.h"
#import "SAPirntTopUpBillModel.h"

@interface SAPirntTopUpBill ()

@property (nonatomic, strong) NSArray *typographyArr;
@property (nonatomic, assign) BOOL isHongKong;

@end

@implementation SAPirntTopUpBill
+ (void)printWithTotalData:(SAPirntTopUpBillModel *)pirntTopUpBillModel printSuccessBlock:(SAPrintSuccessBlock)printSuccessBlock {
    static BOOL isFirst = YES;
    
    SAPirntTopUpBill *this = [[SAPirntTopUpBill alloc] init];
    this.pirntTopUpBillModel = pirntTopUpBillModel;
    this.userInfoModel = [SAValueManger shareInStance].userInfo;
    SACommoditySpecialFeesModel *hkSymbol = [SAOverallSetting shareInstance].hkSymbol;
    this.isHongKong = NO;
    if ([hkSymbol.value isEqualToString:@"1"]) {
        this.isHongKong = YES;
    }
    
    [this printStoreName];// 打印标题头
    [this printLineFeed];// 打印换行
    [this printHeader];// 打印头部
    
    [this printLineFeed];// 打印换行
    [this printLineFeed];// 打印换行
    [this printLineFeed];// 打印换行
    [this printLineFeed];// 打印换行
    [this printPaperCut];// 打印剪纸
    
    [[BluetoothManager sharedInstance] writeToPeripheralWithData:this.writeData success:^{
        NSLog(@"商品入库打印：写入成功");
        if (isFirst) {
            isFirst = NO;
            if (printSuccessBlock) {
                printSuccessBlock();
            }
        }
    } failure:^(NSString * _Nonnull errorMessage) {
        NSLog(@"商品入库打印：写入失败：%@",errorMessage);
        dispatch_async(dispatch_get_main_queue(), ^{
            [Prompt popPromptViewWithMsg:errorMessage duration:2];
        });
    }];
}

// 打印店铺名称
- (void)printStoreName {
    NSString *storeName = @"充值小票";
    NSData *data = [self.escPrinter printCenterText2:storeName font:(PrinterPrintTextFontMiddle)];;
    [self.writeData appendData:data];
}

// 打印头部（单号、申请店、申请人、申请时间）
- (void)printHeader {
    
    NSString *str1 = [NSString stringWithFormat:@"员工: %@",self.pirntTopUpBillModel.employees];;
    NSData *data1 = [self.escPrinter printLeftText:@"" rightText:str1 font:(PrinterPrintTextFontSmall)];
    [self.writeData appendData:data1];
    
    NSString *str2 = [NSString stringWithFormat:@"日期: %@",self.pirntTopUpBillModel.date];
    NSData *data2 = [self.escPrinter printText:str2 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data2];
    
    NSString *str3 = [NSString stringWithFormat:@"会员: %@",self.pirntTopUpBillModel.vipName];
    NSData *data3 = [self.escPrinter printText:str3 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data3];
    
    NSString *str4 = [NSString stringWithFormat:@"手机: %@",self.pirntTopUpBillModel.phone];
    NSData *data4 = [self.escPrinter printText:str4 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data4];
    
    NSString *str5 = [NSString stringWithFormat:@"余额: %@",self.pirntTopUpBillModel.balance];
    NSData *data5 = [self.escPrinter printText:str5 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data5];
    
    NSString *str6 = [NSString stringWithFormat:@"积分: %@",self.pirntTopUpBillModel.integral];
    NSData *data6 = [self.escPrinter printText:str6 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data6];
    
    NSString *str7 = [NSString stringWithFormat:@"充值金额: %@",self.pirntTopUpBillModel.topUp];
    NSData *data7 = [self.escPrinter printText:str7 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data7];
    
    NSString *str8 = [NSString stringWithFormat:@"赠送金额: %@",self.pirntTopUpBillModel.giveAway];
    NSData *data8 = [self.escPrinter printText:str8 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data8];
    
    NSString *str9 = [NSString stringWithFormat:@"结算方式: %@",self.pirntTopUpBillModel.settlementType];
    NSData *data9 = [self.escPrinter printText:str9 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [self.writeData appendData:data9];
}

// 打印分割线
- (void)printPartingLine {
    NSData *data = [self.escPrinter printPartingLine:0];
    [self.writeData appendData:data];
}
// 打印换行
- (void)printLineFeed {
    NSData *data = [self.escPrinter lineFeed];
    [self.writeData appendData:data];
}
// 打印剪纸
- (void)printPaperCut {
    NSData *data = [self.escPrinter paperCut];
    [self.writeData appendData:data];
}

@end
